

package AddressBook.View.Dialog;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionListener;

/**
 * The Class MyInputDialog allows to create a custom input dialog composing by a combobox and 2 buttons
 */
public class MyInputDialog extends JDialog {
    /**
     * This field keeps a reference of the combobox
     */
    private JComboBox myComboBox;

    /**
     * This field keeps a reference of the okButton
     */
    private JButton okButton;

    /**
     * This field keeps a reference of the cancelButton
     */
    private JButton cancelButton;

    /**
     * This constructor create and initialize the input dialog
     * @param owner The parent view
     */
    public MyInputDialog(JFrame owner) {
        super(owner, owner.getTitle(), true);

        JPanel mainPanel = new JPanel(new BorderLayout());
        myComboBox = new JComboBox();

        Border panelBorder = BorderFactory.createEmptyBorder(10, 10, 10, 10);

        okButton = new JButton("Ok");
        cancelButton = new JButton("Cancel");

        mainPanel.setBorder(panelBorder);
        mainPanel.add(myComboBox, BorderLayout.PAGE_START);
        mainPanel.add(cancelButton, BorderLayout.LINE_START);
        mainPanel.add(okButton, BorderLayout.LINE_END);
        add(mainPanel);
        pack();
        setResizable(false);
    }

    /**
     * This method allows to set the prompted text of the combobox
     * @param promptStr The desired text
     */
    public void setText(String promptStr) {
        Border loweredetched = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
        TitledBorder textFieldTitle = BorderFactory.createTitledBorder(
                loweredetched, promptStr);
        myComboBox.setBorder(textFieldTitle);
        pack();

    }

    /**
     * This method allows to attach item to the combobox
     * @param objs The objects to attach
     */
    public void attachItem(Object[] objs) {
        myComboBox.removeAllItems();
        myComboBox.addItem("-");
        for (Object o : objs) myComboBox.addItem(o);
        pack();
        setLocationRelativeTo(null);
    }

    /**
     * This method allows to attach an action listener to the ValidButton
     *
     * @param l The desired actionListener
     */
    public void addActionListenerValidButton(ActionListener l) {
        okButton.addActionListener(l);
    }

    /**
     * This method allows to attach an action listener to the CancelButton
     *
     * @param l The desired actionListener
     */
    public void addActionListenerCancelButton(ActionListener l) {
        cancelButton.addActionListener(l);
    }

    /**
     * This method returns the selected object in the combobox
     * @return The object selected in the combobox
     */
    public Object getSelectedObjectMyComboBox() {
        return myComboBox.getSelectedItem();
    }

}
